<script>
    import region from "@/components/region-picker/data.js";
    import {mapState, mapMutations} from 'vuex';
    export default {
        onLaunch: function() {
            var vm = this;
			global.tempCmp = {};
            global.waitObj = {};
            
            // #ifdef MP-WEIXIN
            vm.upDataApp();
            // #endif

            uni.setStorageSync("region", region);
            vm.getAndLoad('全局配置', vm.WX_ASSETS + '/wxJSON/globalConfig.json', function(res) {
                vm.setGlobalConfig(res);
            });
            vm.doWxLogin(true);
            vm.listenRouter();
        },
        onHide: function() {
            console.log('App Hide')
        },
        onShow: function () {
            this.watchRouter();
        },
        computed: {
            ...mapState(["characterType"]),
        },
        methods: {
        ...mapMutations(['setGlobalConfig']),
         upDataApp: function() { //版本更新
             if (wx.canIUse('getUpdateManager')) { //判断当前微信版本是否支持版本更新
                 const updateManager = wx.getUpdateManager();
         
                 updateManager.onCheckForUpdate(function(res) {
         
                     if (res.hasUpdate) { // 请求完新版本信息的回调
         
                         updateManager.onUpdateReady(function() {
         
                             wx.showModal({
         
                                 title: '更新提示',
         
                                 content: '新版本已经准备好，是否重启应用？',
         
                                 success: function(res) {
         
                                     if (res
                                         .confirm) { // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
         
                                         updateManager.applyUpdate()
         
                                     }
         
                                 }
         
                             })
         
                         });
         
                         updateManager.onUpdateFailed(function() {
         
                             wx.showModal({ // 新的版本下载失败
         
                                 title: '已经有新版本了哟~',
         
                                 content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
         
                             })
         
                         })
         
                     }
         
                 })
         
             }
         },
         watchRouter(name) {
            if (this.characterType) {
                global.styleTimeout && clearTimeout(global.styleTimeout);
                this.changeNavbarColor();
            } else {
                global.styleTimeout = setTimeout(() => {
                    this.watchRouter();
                }, 200);
            }
         },
         changeNavbarColor() {
            if (this.characterType == 1) {
                uni.setNavigationBarColor({
                    frontColor: '#ffffff',
                    backgroundColor: '#FF8800',
                });
            } else if (this.characterType == 2) {
                uni.setNavigationBarColor({
                    frontColor: '#ffffff',
                    backgroundColor: '#5BC68D',
                });
            }
         },
         listenRouter() {
            var that = this;
            uni.addInterceptor('navigateTo', { success(e) { that.watchRouter('navigateTo'); } });
			uni.addInterceptor('redirectTo', { success(e) { that.watchRouter('redirectTo'); } });
            uni.addInterceptor('switchTab', { success(e) { that.watchRouter('switchTab'); } });
            uni.addInterceptor('navigateBack', { success(e) { that.watchRouter('navigateBack'); } });
         }
        }
    }
</script>

<style lang="scss">
    @import "@/uni_modules/uview-ui/index.scss";
    @import "./styles/style.scss";
</style>
